/*
 * @Author: maskMan
 * @LastEditTime: 2025-02-21 08:29:28
 * @Descripttion:网络请求
 */
import { NDefine } from "../types/define";
import axios, { AxiosInstance, RawAxiosRequestHeaders } from "axios";
export default class HttpRequest {
  private static _baseURL = `https://api.weixin.qq.com`;
  private static _resInstance: AxiosInstance;
  private static defaultHead: RawAxiosRequestHeaders = {
    "Content-Type": "application/json"
  };
  /**
   * @description: 创建请求实例
   * @return {*}
   */
  private static _createAxios() {
    try {
      this._resInstance = axios.create({ baseURL: this._baseURL, headers: this.defaultHead, timeout: 10000 });
      // 响应拦截及返回参数格式
      this._resInstance.interceptors.response.use(
        async (res: any) => {
          return res || null;
        },
        (error) => {
          if (error?.response) {
            console.log(error.response);
          }
          return null;
        }
      );
      // 发送拦截
      this._resInstance.interceptors.request.use(
        async (req: any) => {
          return req;
        },
        (error) => {
          if (error?.response) {
            console.error(error.message);
            return error.response;
          }
          return null;
        }
      );
    } catch (error) {
      console.error("请求返回错误数据", error);
    }
  }
  /**
   * @description: get 请求
   * @param {string} url 请求路由
   * @param {object} params 请求参数数据
   * @param {string} baseURL 请求地址
   * @param {RawAxiosRequestHeaders} headers 请求头部
   * @return {*}
   */
  public static get(
    url: string,
    params: NDefine.Obj,
    baseURL: string = this._baseURL,
    headers: RawAxiosRequestHeaders = this.defaultHead
  ): Promise<NDefine.Obj> {
    this._resInstance == undefined && this._createAxios();
    return this._resInstance
      .get(url, { params, baseURL, headers })
      .then(function (data: NDefine.Obj) {
        return data;
      })
      .catch(function (error: any) {
        console.log("请求错误", error);
        return null;
      });
  }
  /**
   * @description: post 请求
   * @param {string} url 请求路由
   * @param {object} params 请求参数数据
   * @param {string} baseURL 请求地址
   * @param {RawAxiosRequestHeaders} headers 请求头部
   * @return {*}
   */
  public static post(
    url: string,
    data: NDefine.Obj,
    baseURL: string = this._baseURL,
    headers: RawAxiosRequestHeaders = this.defaultHead
  ): Promise<NDefine.Obj> {
    this._resInstance == undefined && this._createAxios();
    return this._resInstance
      .post(url, data, { baseURL, headers })
      .then((data: NDefine.Obj) => {
        return data;
      })
      .catch((error: any) => {
        console.log("请求错误", error);
        return null;
      });
  }
  /**
   * @description: delete 请求
   * @param {string} url 请求路由
   * @param {object} params 请求参数数据
   * @param {string} baseURL 请求地址
   * @param {RawAxiosRequestHeaders} headers 请求头部
   * @return {*}
   */
  public static delete(
    url: string,
    params: NDefine.Obj,
    baseURL: string = this._baseURL,
    headers: RawAxiosRequestHeaders = this.defaultHead
  ): Promise<NDefine.Obj> {
    this._resInstance == undefined && this._createAxios();
    return this._resInstance
      .delete(url, { params, baseURL, headers })
      .then(function (data: NDefine.Obj) {
        return data;
      })
      .catch(function (error: any) {
        console.log("请求错误", error);
        return null;
      });
  }
  /**
   * @description: put 请求
   * @param {string} url 请求路由
   * @param {object} params 请求参数数据
   * @param {string} baseURL 请求地址
   * @param {RawAxiosRequestHeaders} headers 请求头部
   * @return {*}
   */
  public static put(
    url: string,
    data: NDefine.Obj,
    baseURL: string = this._baseURL,
    headers: RawAxiosRequestHeaders = this.defaultHead
  ): Promise<NDefine.Obj> {
    this._resInstance == undefined && this._createAxios();
    return this._resInstance
      .put(url, data, { baseURL, headers })
      .then((data: NDefine.Obj) => {
        return data;
      })
      .catch((error: any) => {
        console.log("请求错误", error);
        return null;
      });
  }
}
